<?php

namespace JsonLowCode\Traits\Events;

trait HasLifeCycle
{
    /**
     * 设置生命周期函数
     * @param string $key 函数名
     * @param string $value 完整的函数
     * @return $this
     */
    public function lifecycle(string $key, string $value): static
    {
        $this->lifeCycles[$key] = JSFunction($value);
        return $this;
    }

    /**
     * setup
     * @param string $value
     * @return $this
     */
    public function setup(string $value): static
    {
        $this->lifeCycles['setup'] = JSFunction($value);
        return $this;
    }

    /**
     * created
     * @param string $value
     * @return $this
     */
    public function created(string $value): static
    {
        $this->lifeCycles['created'] = JSFunction($value);
        return $this;
    }

    /**
     * beforeCreate
     * @param string $value
     * @return $this
     */
    public function beforeCreate(string $value): static
    {
        $this->lifeCycles['beforeCreate'] = JSFunction($value);
        return $this;
    }

    /**
     * initProps 初始化
     * @param array|string $props
     * @return $this
     */
    public function initProps(array|string $props): static
    {
        $this->lifeCycles['initProps'] = $props;
        return $this;
    }

    /**
     * 设置emits
     * @param array $emits
     * @return $this
     */
    public function initEmits(array $emits = []): static
    {
        $this->initEmits = array_unique(array_merge($this->initEmits,$emits));
        $this->lifeCycles['initEmits'] = JSExpression(json_encode($this->initEmits));
        return $this;
    }

    /**
     * initProvide 初始化
     * @param array|string $value
     * @return $this
     */
    public function initProvide(array|string $value): static
    {
        $this->lifeCycles['initProvide'] = $value;
        return $this;
    }

    /**
     * initProvide 初始化
     * @param array|string $value
     * @return $this
     */
    public function initInject(array|string $value): static
    {
        $this->lifeCycles['initInject'] = $value;
        return $this;
    }

    /**
     * initData 初始化
     * @param string $key
     * @param string $value
     * @return $this
     */
    public function initData(string $key,string $value): static
    {
        $this->lifeCycles['initData'][$key] = $value;
        return $this;
    }


    /**
     * watch 监听
     * @param string $key
     * @param string $value
     * @param bool $isJSExpression
     * @return $this
     */
    public function initWatch(string $key, string $value, bool $isJSExpression = false): static
    {
        $this->lifeCycles['initWatch'][$key] = $isJSExpression? JSExpression($value):JSFunction($value);
        return $this;
    }

    /**
     * Computed 监听
     * @param string $key
     * @param string $value
     * @param bool $isJSExpression
     * @return $this
     */
    public function initComputed(string $key, string $value, bool $isJSExpression = false): static
    {
        $this->lifeCycles['initComputed'][$key] = $isJSExpression? JSExpression($value):JSFunction($value);
        return $this;
    }


    /**
     * 设置beforeMount生命周期函数
     * @param string $value js代码
     * @return $this
     */
    public function onBeforeMount(string $value): static
    {
        return $this->lifecycle('beforeMount',$value);
    }

    /**
     * 设置mounted生命周期函数
     * @param string $value js代码
     * @return $this
     */
    public function onMounted(string $value): static
    {
        return $this->lifecycle('mounted',$value);
    }

    /**
     * 设置beforeUpdate生命周期函数
     * @param string $value 完整的函数
     * @return $this
     */
    public function onBeforeUpdate(string $value): static
    {
        return $this->lifecycle('beforeUpdate',$value);
    }

    /**
     * 设置beforeUpdate生命周期函数
     * @param string $value 完整的函数
     * @return $this
     */
    public function onUpdated(string $value): static
    {
        return $this->lifecycle('updated',$value);
    }

    /**
     * 设置beforeUnmount生命周期函数
     * @param string $value 完整的函数
     * @return $this
     */
    public function onBeforeUnmount(string $value): static
    {
        return $this->lifecycle('beforeUnmount',$value);
    }

    /**
     * 设置beforeUnmount生命周期函数
     * @param string $value 完整的函数
     * @return $this
     */
    public function onUnmounted(string $value): static
    {
        return $this->lifecycle('unmounted',$value);
    }

    /**
     * 设置beforeUnmount生命周期函数
     * @param string $value 完整的函数
     * @return $this
     */
    public function onErrorCaptured(string $value): static
    {
        return $this->lifecycle('errorCaptured',$value);
    }
}
